package itsm.isperp.module.controller.customer;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppDepartment;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.service.app.AppDepartmentService;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.customer.CusCustomerService;
import itsm.isperp.module.service.project.ProjProjectService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("customer/user")
public class CusUserController {

	@Autowired
	protected AppUserService appUserService;

	public AppUserService getService() {
		return appUserService;
	}

	@Autowired
	protected AppDictService appDictService;

	/**
	 * 用户编辑界面
	 * 
	 * @return 用户编辑界面视图
	 */
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {

		return null;
	}

	@RequestMapping("list")
	@ResponseBody
	public PageResponse<Map<String,Object>> list(JqGridRequest request,
			HttpServletRequest req) {
		String customerId = req.getParameter("customerId");
		if (StringUtils.isNotEmpty(customerId)) {

			request.addFilterRule("customerId", JqGridFilterRuleOp.eq,
					customerId);
		}

		String term = req.getParameter("term");

		if (StringUtils.isNotEmpty(term)) {
			request.isPageable(false);
			request.setRows(15);

			request.setSearchString(" a.name LIKE '%" + term
					+ "%' OR  fullname LIKE '%" + term + "%' OR email LIKE '%"
					+ term + "%'  OR a.tel LIKE '%" + term
					+ "%' OR mobile LIKE '%" + term + "%' OR b.treenode_name_path like '%"+term+"%'");

			return this.getService().findAllDetailByRequest(request).toPage();

		} else {
			return this.getService().findAllDetailByRequest(request).toPage();
		}

	}

	@RequestMapping("createUserEdit")
	public ModelAndView createUserEdit(String id) {
		ModelAndView mv = new ModelAndView("customer/user/createUserEdit");
		AppUser model = null;
		String editMode="true";
		if (StringUtils.isEmpty(id)) {
			model = new AppUser();

			model.setId(HibernateUUIDGenerator.generate());

			model.setGender("男");
			model.setEnable(true);
			editMode="false";
		} else {
			model = this.getService().findOne(id);
			AppDepartmentService appDepartmentService = ContextHolder
					.getSpringBean("appDepartmentService");
			AppDepartment dept = appDepartmentService.findOne(model
					.getAppDepartmentId());

			mv.addObject("deptName",
					dept == null ? null : dept.getTreenodeNamePath());

			mv.addObject("model", model);
		}
		mv.addObject("editMode", editMode);
		mv.addObject("model", model);
		mv.addObject("sex", appDictService.findByType("SEX"));
		mv.addObject("type", appDictService.findByType("USER_TYPE"));
		return mv;
	}

	/**
	 * 获取用户的管理关联项目
	 */
	@RequestMapping("listProjects")
	@ResponseBody
	public List<Map<String, Object>> listProjects(String userId) {
	  ProjProjectService projProjectService = ContextHolder
        .getSpringBean("projProjectService");
		if(StringUtils.isEmpty(userId))
		{
		  return projProjectService.findAllDetailPublished("id","name");
		}else
		{
			List<Map<String,Object>> list=appUserService.findUserProjects(userId);
			if(list.size()>0)
			{
				return list;
			}else
			{
			  List<Map<String,Object>> list2=this.getService().findMembersProjectByUserId(userId);
			  if(list2.size()>0)
			    return list2;
			  else
			  {
			    return projProjectService.findAllDetailPublished("id","name");
			  }
			}
		}
		
	}

	/**
	 * 用户查看界面
	 * 
	 * @return 用户查看界面
	 */
	@RequestMapping("view")
	public ModelAndView view(String id, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> cusUser = appUserService.findOneDetailById(id);

		mv.addObject("model", cusUser);

		return mv;
	}

	@RequestMapping("getDetail")
	@ResponseBody
	public Map<String, Object> getDetail(String id) {
		return appUserService.findOneDetailById(id);
	}

	@RequestMapping("getTree")
	@ResponseBody
	public List<? extends ITreeNode> getTree(String parentId) {

		AppDepartmentService appDepartmentService = ContextHolder
				.getSpringBean("appDepartmentService");

		CusCustomerService cusCustomerService = ContextHolder
				.getSpringBean("cusCustomerService");

		Map<String, Object> map = cusCustomerService.findOneToMap(parentId,
				"department_id");
		String pId = null;
		if (map != null) {
			Object p = map.get("department_id");

			if (p != null) {
				pId = (String) p;

				return appDepartmentService.getTree(pId);
			}
		}

		return null;

	}

	@RequestMapping("getUser")
	@ResponseBody
	public Map<String, Object> getUser(String id) {
		Map<String, Object> cusUser = appUserService.findOneDetailById(id);
		return cusUser;
	}
}
